<template>
  <div v-if="visible" class="modal-overlay" @click="handleClose">
    <div class="modal-container" @click.stop>
      <div class="modal-header">
        <h3 class="modal-title">{{ model?.productName || '3D模型预览' }}</h3>
        <button class="close-btn" @click="handleClose">&times;</button>
      </div>
      
      <div class="modal-body">
        <!-- 模型信息 -->
        <div class="model-info-section" v-if="model">
          <div class="info-row">
            <span class="info-label">产品名称：</span>
            <span class="info-value">{{ model.productName }}</span>
          </div>
          <div class="info-row">
            <span class="info-label">分类：</span>
            <span class="info-value">{{ getCategoryName(model.category) }}</span>
          </div>
          <div class="info-row" v-if="model.description">
            <span class="info-label">描述：</span>
            <span class="info-value">{{ model.description }}</span>
          </div>
        </div>
        
        <!-- 3D模型预览 -->
        <div class="model-viewer-wrapper">
          <div v-if="isLoading" class="loading-overlay">
            <div class="loading-spinner"></div>
            <p>正在加载3D模型...</p>
          </div>
          <model-viewer
            v-if="model?.fileUrl"
            :src="model.fileUrl"
            :alt="model.productName"
            camera-controls
            auto-rotate
            auto-rotate-delay="1000"
            interaction-policy="allow-when-focused"
            shadow-intensity="1"
            environment-image="neutral"
            style="width: 100%; height: 500px; background-color: #f0f0f0;"
            @load="onModelLoad"
            @error="onModelError"
          >
            <div slot="progress-bar" class="progress-bar">
              <div class="progress-bar__bar"></div>
            </div>
          </model-viewer>
        </div>
      </div>
      
      <div class="modal-footer">
        <button class="btn-primary" @click="handleClose">关闭</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits, onMounted, ref, watch } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    required: true
  },
  model: {
    type: Object,
    default: null
  }
})

const emit = defineEmits(['close'])

const isLoading = ref(true)

const handleClose = () => {
  emit('close')
}

const onModelLoad = () => {
  isLoading.value = false
}

const onModelError = () => {
  isLoading.value = false
}

// 监听模型变化，重置加载状态
watch(() => props.model, () => {
  isLoading.value = true
}, { immediate: true })

const getCategoryName = (category) => {
  const categoryMap = {
    'architecture': '建筑模型',
    'furniture': '家具模型',
    'vehicle': '交通工具',
    'character': '角色模型',
    'weapon': '武器模型',
    'nature': '自然景观',
    'electronics': '电子产品',
    'other': '其他'
  }
  return categoryMap[category] || category
}

// 动态导入 model-viewer，避免重复注册
onMounted(async () => {
  if (!customElements.get('model-viewer')) {
    await import('@google/model-viewer')
  }
})
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-container {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 900px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 25px;
  border-bottom: 1px solid #eee;
}

.modal-title {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.close-btn {
  background: none;
  border: none;
  font-size: 28px;
  cursor: pointer;
  color: #999;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s;
  padding: 0;
}

.close-btn:hover {
  background: #f5f5f5;
  color: #333;
}

.modal-body {
  padding: 25px;
  overflow-y: auto;
}

.model-info-section {
  margin-bottom: 20px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
}

.info-row {
  display: flex;
  margin-bottom: 10px;
}

.info-row:last-child {
  margin-bottom: 0;
}

.info-label {
  font-weight: 500;
  color: #666;
  min-width: 80px;
}

.info-value {
  color: #333;
  flex: 1;
}

.model-viewer-wrapper {
  margin-top: 20px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #e5e5e5;
  height: 500px;
  position: relative;
}

.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(248, 249, 250, 0.9);
  color: #666;
  z-index: 10;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #e5e5e5;
  border-top: 3px solid #3742fa;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  padding: 20px 25px;
  border-top: 1px solid #eee;
  background: #f8f9fa;
}

.btn-primary {
  background: #3742fa;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s;
}

.btn-primary:hover {
  background: #2f3af2;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(55, 66, 250, 0.3);
}
</style>

